package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import Utilities.EmailClient;

import ExceptionClasses.CantAccessEmailException;

@WebServlet("/GetEmailContentServlet")
public class GetEmailContentServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		int index = Integer.valueOf(request.getParameter("index"));
		String business = (String) request.getSession().getAttribute("username");
		
		try {
			EmailClient client = new EmailClient(business);
			String content = client.openMessage(index);
			JSONObject result = new JSONObject();
			result.put("content", content);
			response.getWriter().print(result.toString());
		} 
		
		catch (CantAccessEmailException e) {
			e.printStackTrace();
		}
		
		catch (IOException e) {
			e.printStackTrace();
		}
		
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
